using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;

namespace UserInterface.Misc
{
    public class EncryptTools
    {
        private const int PassPhraseLength = 10;
        private const string EncryptPassPhraseSessionKey = "encrypt_pass_phrase";
        
        private static string passPhrase;
        
        static EncryptTools()
        {
            HttpContext context = HttpContext.Current;
            HttpSessionState session = context.Session;
            if (session[EncryptPassPhraseSessionKey] == null)
            {
                passPhrase = RandomPassword.Generate(PassPhraseLength);
                session[EncryptPassPhraseSessionKey] = passPhrase;
            }
            else
            {
                passPhrase = (string)session[EncryptPassPhraseSessionKey];
            }
        }
        
        public static string Encrypt(string text)
        {
            return new RijndaelEnhanced(passPhrase).Encrypt(text);
        }

        public static string Decrypt(string text)
        {
            return new RijndaelEnhanced(passPhrase).Decrypt(text);
        }
    }
}
